﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using ModbusReadAndWrite.MasterStation;
using ModbusReadAndWrite.Protocol;
using ModbusReadAndWrite.SlaveStation;

namespace ModbusReadAndWrite
{
    class Program
    {
        public static List<TCPProtocol> tcpLinkedList = new List<TCPProtocol>();
        public static List<ModbusProtocol> modbusLinkedList = new List<ModbusProtocol>();

        static void Main(string[] args)
        {
            byte[] messages = new byte[]{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20};

            test(messages);
        }

        //[TestMethod]
        public static void test(byte[] messages)
        {
            foreach (var message in messages)
            {
                Console.Write("{0}\t", message);
            }
            //Console.WriteLine("\nmessages' length is:{0}", messages.Length);

            Master master = new Master(1, messages);
            //Console.WriteLine(master.ToString());

            ClickAddHandle(2);

            Slave slave = new Slave();

            Console.WriteLine();

            Console.ReadKey();
        }

        public static void ClickAddHandle(int num)
        {
            int clickProtocol = num;
            switch (clickProtocol)
            {
                case (int)ProtocolType.ModbusRTUProtocol:
                    Console.WriteLine("RTU");
                    ModbusProtocol modbusRTU = new ModbusProtocol(0);
                    modbusLinkedList.Add(modbusRTU);
                    break;
                case (int)ProtocolType.ModbusASCProtocol:
                    Console.WriteLine("ASC");
                    ModbusProtocol modbusASC = new ModbusProtocol(1);
                    modbusLinkedList.Add(modbusASC);
                    break;
                case (int)ProtocolType.ModbusTCPProtocol:
                    Console.WriteLine("TCP");

                    bool flag = true;
                    tcpLinkedList.Add(IsModified(true));
                    TCPProtocol tcp = (TCPProtocol)tcpLinkedList[0];
                    Console.WriteLine(tcp.IpAddress);
                    break;
                default:
                    //错误协议弹出错误消息框
                    Console.WriteLine("Wrong Protocol...");
                    return;
            }
        }

        public static TCPProtocol IsModified(bool flag)
        {
            if (!flag) return new TCPProtocol();

            //读取界面中的值
            string ipAddress = "192.168.210.17";
            int port = 8888;
            int timeout = 1000;
            string ipType = "Ipv6";

            bool isLongTimeCon = false;
            int keepTimeout = 1000;

            return new TCPProtocol(ipAddress, port, timeout, ipType, isLongTimeCon, keepTimeout);
        }
    }
}
